""" System imports """
import logging
import pygame

""" Equinox """
from events import *
from sprites import *
""" Constants """


""" Set up logging """
_log = logging.getLogger("equinox.view")


class View:
    """ Superclass for all views """
    def __init__(self, evManager):
        self.evManager = evManager
        self.evManager.registerListener( self )

    #----------------------------------------------------------------------
    def notify(self, event):
        if isinstance( event, TickEvent ):
            # Draw everything
            pass
        
class MapView(View):
    
    def __init__(self, evManager):
        self.evManager = evManager
        self.evManager.registerListener(self)
        
        pygame.init()
        self.screen = pygame.display.set_mode((640,480))
        pygame.display.set_caption("Equinox Engine")
        
        self.background = pygame.surface.Surface(self.screen.get_size())
        self.background.fill((0,0,0))
        
        self.bottom = pygame.sprite.LayeredUpdates()
        self.top = pygame.sprite.LayeredUpdates()
        
    
    def notify(self, event):
        
        if isinstance(event, TickEvent):
            #Draw the map
            self.bottom.clear(self.screen, self.background)
            self.top.clear(self.screen, self.background)
            
            self.bottom.update()
            self.top.update()
            
            self.updateRects = self.bottom.draw(self.screen) + self.top.draw(self.screen)
            pygame.display.update(self.updateRects)
        
        elif isinstance(event, MapBuiltEvent):
            self.map = event.map
            self.showMap()
        
        elif isinstance(event, CharacterPlaceEvent):
            pass
        
        
    def showMap(self):
        """ Add the map tiles to the background group """
        pass
        
class PygameView:
    def __init__(self, evManager):
        self.evManager = evManager
        self.evManager.registerListener( self )

        pygame.init()
        self.window = pygame.display.set_mode( (424,440) )
        pygame.display.set_caption( 'Equinox Engine - Test Equinox' )
        self.background = pygame.Surface( self.window.get_size() )
        self.background.fill( (0,0,0) )
        font = pygame.font.Font(None, 30)
        text = """Press SPACE BAR to start"""
        textImg = font.render( text, 1, (255,0,0))
        self.background.blit( textImg, (0,0) )
        self.window.blit( self.background, (0,0) )
        pygame.display.flip()

        self.backSprites = pygame.sprite.RenderUpdates()
        self.frontSprites = pygame.sprite.RenderUpdates()


    #----------------------------------------------------------------------
    def showMap(self, gameMap):
        # clear the screen first
        self.background.fill( (0,0,0) )
        self.window.blit( self.background, (0,0) )
        pygame.display.flip()

        for row in gameMap.sectors:
            for sector in row:
                newSprite = SectorSprite( sector, self.backSprites )
                newSprite.rect = pygame.rect.Rect(SPRITE_WIDTH * sector.coords[0], SPRITE_HEIGHT * sector.coords[1], SPRITE_WIDTH, SPRITE_HEIGHT )
                newSprite = None

    #----------------------------------------------------------------------
    def showCharacter(self, character):
        sector = character.sector
        characterSprite = CharacterSprite( self.frontSprites )
        sectorSprite = self.getSectorSprite( sector )
        characterSprite.rect.center = sectorSprite.rect.center

    #----------------------------------------------------------------------
    def moveCharacter(self, character):
        characterSprite = self.getCharacterSprite( character )

        sector = character.sector
        sectorSprite = self.getSectorSprite( sector )

        characterSprite.moveTo = sectorSprite.rect.center

    #----------------------------------------------------------------------
    def getCharacterSprite(self, charactor):
        #there will be only one
        for s in self.frontSprites:
            return s
        return None

    #----------------------------------------------------------------------
    def getSectorSprite(self, sector):
        for s in self.backSprites:
            if hasattr(s, "sector") and s.sector == sector:
                return s


    #----------------------------------------------------------------------
    def notify(self, event):
        if isinstance( event, TickEvent ):
            #Draw Everything
            self.backSprites.clear( self.window, self.background )
            self.frontSprites.clear( self.window, self.background )

            self.backSprites.update()
            self.frontSprites.update()

            dirtyRects1 = self.backSprites.draw( self.window )
            dirtyRects2 = self.frontSprites.draw( self.window )

            dirtyRects = dirtyRects1 + dirtyRects2
            pygame.display.update( dirtyRects )


        elif isinstance( event, MapBuiltEvent ):
            _log.debug("Showing map")
            gameMap = event.map
            self.showMap( gameMap )

        elif isinstance( event, CharacterPlaceEvent ):
            self.showCharacter( event.character )

        elif isinstance( event, CharacterMoveEvent ):
            self.moveCharacter( event.character )
